import os

from google.appengine.ext import db
from google.appengine.ext.webapp import template

from model import Contact

from tools import manager

class ContactManager(manager.Manager):
    """
    """
    def show(self, contact_key):
        """
        """
        #
        contact = Contact().get(contact_key)

        # set the template for edit
        self.template_values['action'] = 'contact'
        self.template_values['contact'] = contact

        # find the template
        path = os.path.join(os.path.dirname(__file__),
                                '../templates', 'contact', 'index.html')

        # display the contact
        return template.render(path, self.template_values)

    def edit(self, contact_key):
        """
        """
        #
        contact = Contact().get(contact_key)

        # set the template for edit
        self.template_values['contact'] = contact
        self.template_values['page'] = 'contact'
        self.template_values['images'] = self.get_content_images()

        # find the template
        path = os.path.join(os.path.dirname(__file__),
                            '../templates', 'contact', 'edit.html')

        # display the contact
        return template.render(path, self.template_values)

    def new(self):
        """
        """
        # create the new contact
        contact = Contact()

        # set the language
        contact.language = self.language

        #
        contact.put()

        # return the key to work on it
        return contact.key()

    def save(self, contact_key, info):
        """
        """
        #
        contact = Contact().get(contact_key)

        # set info
        contact.info = info
        contact.html_info = self.get_html_content(info)

        # save contact
        contact.put()
